//
//  MainViewController.m
//  CWeekly
//
//  Created by Tiger on 12-4-5.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MainViewController.h"
#import "CTabBarItem.h"
#import "DetailViewController.h"
#import "FormDetailViewController.h"
#import <QuartzCore/QuartzCore.h>
#define HEIGHT_S 1024-75-78
#define HEIGHT_H 768-75-78
#define HEIGHT_S_2 (1024-75-78)/2
#define HEIGHT_H_2 (768-75-78)/2

@interface MainViewController ()

@end

@implementation MainViewController
@synthesize  titleLabel,popviewContoller,topBar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginEvent:) name:LOGINED object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(signupEvent:) name:SIGN_UP object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignEvent:) name:RESIGN object:nil];
        
        firstLoad = YES;
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:LOGINED object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SIGN_UP object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:RESIGN object:nil];
    [viewsArray release];
    [loginView release];
    [searchVC release];
    [helpVC release];
    [vc release];
    [popviewContoller release];
    [bar release];
    [sdView release];
    [sjView release];
    [scView release];
    [topBar release];
    [titleLabel release];
    [loadingImage release];
    [super dealloc];
}
#pragma mark notifications
-(void)resignEvent:(NSNotification*)notify{
    
}
-(void)loginEvent:(NSNotification*)notify{
//    NSDictionary* dic=[notify object];
//    UIBarButtonItem* btnItem=topBar.loginItem;
//    UIButton* la=[UIButton buttonWithType:UIButtonTypeCustom];
//    la.frame=CGRectMake(0, 0, 75, 35);
//    la.backgroundColor=[UIColor clearColor];
//    la.tag=100;
//    NSString* uid=[NSString stringWithFormat:@"用户:%@",[dic valueForKey:@"userId"]];
//    [la setTitle:uid forState:UIControlStateNormal];
//    [la setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    la.titleLabel.adjustsFontSizeToFitWidth=YES;
//    btnItem.customView=la;
}

-(void)signupEvent:(NSNotification*)notify{

}
#pragma mark viewlifecycle
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=YES;
    if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        bar.frame = CGRectMake(0, HEIGHT_H+20+55,1024, 58);
        self.view.backgroundColor = MAIN_BG_COLOR;
        topBar.frame=CGRectMake(0, 0, 1024, 55);
        loginView.frame=CGRectMake(0, 55, 1024, HEIGHT_H);
        
        loadingImage.frame = CGRectMake(0, 20, 1024, 748);
        loadingImage.image = [UIImage imageNamed:@"Default-Landscape.png"];
    }
    else {  
        bar.frame = CGRectMake(0, HEIGHT_S+20+55,768, 58);
        self.view.backgroundColor = MAIN_BG_V_COLOR;
        topBar.frame=CGRectMake(0, 0, 768, 55);
        loginView.frame=CGRectMake(0, 55, 768, HEIGHT_S);
        loadingImage.frame = CGRectMake(0, 20, 768, 1004);
        loadingImage.image = [UIImage imageNamed:@"Default-Portrait.png"];
    }
    [loginView setNeedsDisplay];
    CGSize topSize=topBar.frame.size;
    titleLabel.center=CGPointMake(topSize.width/2, topSize.height/2);
    [sdView willRotateToInterfaceOrientation:self.interfaceOrientation];
    [sjView willRotateToInterfaceOrientation:self.interfaceOrientation];
    [scView willRotateToInterfaceOrientation:self.interfaceOrientation];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
//    self.navigationController.navigationBar.tintColor=[UIColor clearColor];
    
    [[NSNotificationCenter defaultCenter] addObserver: self 
                                             selector: @selector(clearLoadingImage) 
                                                 name: @"ClearLoadingImage" 
                                               object: nil ];
    
    [[NSNotificationCenter defaultCenter] addObserver: self 
                                             selector: @selector(toDetaiViewController:) 
                                                 name: @"ToDetaiViewController" 
                                               object: nil ];

    self.view.autoresizesSubviews = NO;
    if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        isH = YES;
    }
    else {
        isH = NO;
    }
    viewsArray=[[NSMutableArray alloc] init];
    loginView=[[LoginView alloc] initWithH:isH];
    loadingImage = [[UIImageView alloc]init];
    if (isH) {
        loadingImage.frame = CGRectMake(0, 20, 1024, 748);
        loadingImage.image = [UIImage imageNamed:@"Default-Landscape.png"];
    }
    else {
        loadingImage.frame = CGRectMake(0, 20, 768, 1004);
        loadingImage.image = [UIImage imageNamed:@"Default-Portrait.png"];
    }
    
    [self.navigationController.view addSubview:loadingImage];
    
    sdView = [[SDView alloc]initWithH:isH];//商店
    sjView = [[SJView alloc]initWithH:isH];//书架
    scView = [[SCView alloc]initWithH:isH];//收藏
    loginView.alpha=0;
    loginView.tag=44;

    sjView.alpha = 0.0;
    sjView.tag=55;
    sdView.tag=66;
    scView.alpha = 0.0;
    scView.tag=77;
    [self.view addSubview:sdView];
    [self.view addSubview:sjView];
    [self.view addSubview:scView];
    [self.view addSubview:loginView];
    
    [viewsArray addObject:sdView];
    [viewsArray addObject:sjView];
    [viewsArray addObject:scView];
    
    topBar=[[CTopBar alloc] initWithFrame:CGRectZero];
    topBar.delegate=self;
    [self.view addSubview:topBar];
    topBar.titleLabel.text = @"商店";

    bar=[[CTabBar alloc] initWithFrame:CGRectZero];
    bar.delegate=self;
    [self.view addSubview:bar];

    if (isH) {
        bar.frame = CGRectMake(0, HEIGHT_H+20+55,1024, 58);
        self.view.backgroundColor = MAIN_BG_COLOR;
        topBar.frame=CGRectMake(0, 0, 1024, 55);
        loginView.frame=CGRectMake(0, 55, 1024, HEIGHT_H);
    }
    else {  
        bar.frame = CGRectMake(0, HEIGHT_S+20+55,768, 58);
        self.view.backgroundColor = MAIN_BG_V_COLOR;
        topBar.frame=CGRectMake(0, 0, 768, 55);
        loginView.frame=CGRectMake(0, 55, 768, HEIGHT_S);
    }
    CGSize topSize=topBar.frame.size;
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 200, 40)];
    titleLabel.text = @"商店";
    titleLabel.textAlignment = UITextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:22.0];
    titleLabel.backgroundColor = [UIColor clearColor];
    [topBar addSubview: titleLabel];
    titleLabel.center=CGPointMake(topSize.width/2, topSize.height/2);
    firstLoad = NO;
    UIBarButtonItem* shopItem=[bar.barButtonItems objectAtIndex:0];
    CTabBarItem*shopBtn=(CTabBarItem*)shopItem.customView;
    [bar itemSelected:shopBtn];

}

- (void)toDetaiViewController:(NSNotification *)notify
{
    FormDetailViewController *detailViewController = [[FormDetailViewController alloc]init];
    detailViewController.mgid=[[notify object] objectForKey:@"mgId"];
    [self.navigationController pushViewController:detailViewController animated:YES];
    if ([[notify object] objectForKey:@"num"]) {
        [detailViewController setBookPage:[[[notify object] objectForKey:@"num"] intValue]];
    }
    [detailViewController release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}
#pragma mark -
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {  
    
    if (!firstLoad) {
        [sdView willRotateToInterfaceOrientation:toInterfaceOrientation];
        [sjView willRotateToInterfaceOrientation:toInterfaceOrientation];
        [scView willRotateToInterfaceOrientation:toInterfaceOrientation];
        
        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            bar.frame = CGRectMake(0, HEIGHT_H+20+55,1024, 58);
            self.view.backgroundColor = MAIN_BG_COLOR;
            topBar.frame=CGRectMake(0, 0, 1024, 55);
            loginView.frame=CGRectMake(0, 55, 1024, HEIGHT_H);
            
            loadingImage.frame = CGRectMake(0, 20, 1024, 748);
            loadingImage.image = [UIImage imageNamed:@"Default-Landscape.png"];
        }
        else {  
            bar.frame = CGRectMake(0, HEIGHT_S+20+55,768, 58);
            self.view.backgroundColor = MAIN_BG_V_COLOR;
            topBar.frame=CGRectMake(0, 0, 768, 55);
            loginView.frame=CGRectMake(0, 55, 768, HEIGHT_S);
            loadingImage.frame = CGRectMake(0, 20, 768, 1004);
            loadingImage.image = [UIImage imageNamed:@"Default-Portrait.png"];
        }

    }   
    CGSize topSize=topBar.frame.size;
    titleLabel.center=CGPointMake(topSize.width/2, topSize.height/2);
    [loginView setNeedsDisplay];
    [topBar setNeedsDisplay];
    [bar setNeedsDisplay];
}
-(void)showView:(UIView*)destView{
    for (UIView* u in viewsArray) {
        if ([u isEqual:destView]) {
            u.alpha=1;
        }else {
            u.alpha=0;
        }
    }
}

- (void)clearLoadingImage
{
    [loadingImage removeFromSuperview];
}

#pragma mark BottomBardelegate
-(void)barSelectAtIndex:(NSInteger)index{
    switch (index) {
        case 1000:
//            sdView.alpha=1;
//            sjView.alpha=0;
//            scView.alpha=0;
            [self showView:sdView];
            titleLabel.text = @"商店";
            break;
            
        case 1001:
//            sdView.alpha=0;
//            sjView.alpha=1;
//            scView.alpha=0;
            [self showView:sjView];
           titleLabel.text = @"书架";
            break;
            
        case 1002:
//            sdView.alpha=0;
//            sjView.alpha=0;
//            scView.alpha=1;
            [self showView:scView];
           titleLabel.text = @"收藏";
            break;
            
        default:
            break;
    }
}

#pragma mark TopBardelegate
-(void)topBar:(CTopBar*)topbar DidSelectButton:(UIButton*)btnSelected AtIndex:(NSInteger)index{ 
    
    if (vc==nil) {
        vc=[[UIViewController alloc] init];
    }
    if (popviewContoller==nil) {
        popviewContoller=[[UIPopoverController alloc] initWithContentViewController:vc];
        popviewContoller.delegate=self;
    }
    if (btnSelected.selected) {
        if (index==100) {
            [loginView setTransform:CGAffineTransformMakeScale( 1.0f, 1.0f)];

            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.3];
            loginView.alpha=0;
            [loginView setTransform:CGAffineTransformMakeScale( 0.1f, 0.1f)];
            [UIView commitAnimations];

        }
        if ([popviewContoller isPopoverVisible]) {
            [popviewContoller dismissPopoverAnimated:YES];
        }
    }else {
        switch (index) {
            case 100:
//                vc.view.backgroundColor=[UIColor blueColor];
//                [popviewContoller presentPopoverFromBarButtonItem:topBar.loginItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
                [loginView setTransform:CGAffineTransformMakeScale( 0.1f, 0.1f)];
                loginView.alpha=0.5;
                [UIView beginAnimations:nil context:NULL];
                [UIView setAnimationDuration:0.3];
                [loginView setTransform:CGAffineTransformMakeScale( 1.0f, 1.0f)];

                loginView.alpha=1;
                [UIView commitAnimations];
                if ([popviewContoller isPopoverVisible]) {
                    [popviewContoller dismissPopoverAnimated:YES];
                }
                topSelectedTag=100;
                break;
            case 101:
                if (searchVC==nil) {
                    searchVC=[[SeachViewController alloc] init];
                }
                [popviewContoller setContentViewController:searchVC];
                [popviewContoller setPopoverContentSize:CGSizeMake(320, 500)];

                [popviewContoller presentPopoverFromBarButtonItem:topBar.searchItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
                topSelectedTag=101;
                [loginView closeLogin];
                break;
            case 102:
                if (helpVC==nil) {
                    helpVC=[[HelpViewController alloc] init];
                }
                [popviewContoller setPopoverContentSize:helpVC.view.frame.size];
                [popviewContoller setContentViewController:helpVC];
                [popviewContoller presentPopoverFromBarButtonItem:topBar.helpItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
                topSelectedTag=102;
                [loginView closeLogin];
                break;            
            default:
                break;
        }
    }
}
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
    UIButton* tempBtn;
    switch (topSelectedTag) {
        case 100:
            tempBtn=(UIButton*)topBar.loginItem.customView;
            break;
        case 101:
            tempBtn=(UIButton*)topBar.searchItem.customView;
            break;
        case 102:
            tempBtn=(UIButton*)topBar.helpItem.customView;
            break;
        default:
            break;
    }
    [tempBtn setSelected:NO];

}
@end
